package cn.lll.vuehouse.handler;


import cn.lll.vuehouse.common.ResponseResult;
import cn.lll.vuehouse.common.ResponseStatus;
import com.auth0.jwt.exceptions.JWTDecodeException;
import com.auth0.jwt.exceptions.SignatureVerificationException;
import com.auth0.jwt.exceptions.TokenExpiredException;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/*
* 全局异常处理
* */
@RestControllerAdvice
public class GlobalExceptionHandler {

    //用户名不存在
    @ExceptionHandler(UnknownAccountException.class)
    public ResponseResult unknownAccountExceptionHandler(UnknownAccountException e){
        e.printStackTrace();
        ResponseResult responseResult = new ResponseResult();
         responseResult.fail(ResponseStatus.UNKNOWN_ACCOUNT);
         return responseResult;
    }


    //密码错误
    @ExceptionHandler(IncorrectCredentialsException.class)
    public ResponseResult incorrectCredentialsExceptionHandler(IncorrectCredentialsException e){
        e.printStackTrace();
        ResponseResult responseResult = new ResponseResult();
        responseResult.fail(ResponseStatus.INCORRECT_CREDENTIALS);
        return responseResult;
    }


    //用户被禁用
    @ExceptionHandler(LockedAccountException.class)
    public ResponseResult lockedAccountExceptionHandler(LockedAccountException e){
        e.printStackTrace();
        ResponseResult responseResult = new ResponseResult();
        responseResult.fail(ResponseStatus.LOCK_ACCOUNT);
        return responseResult;
    }


    //token失效
    @ExceptionHandler(TokenExpiredException.class)
    public ResponseResult tokenExpiredExceptionHandler(TokenExpiredException e){
        e.printStackTrace();
        ResponseResult responseResult = new ResponseResult();
        responseResult.fail(ResponseStatus.TOKEN_EXPIRED);
        return responseResult;
    }

    //token异常
    @ExceptionHandler({SignatureVerificationException.class, JWTDecodeException.class})
    public ResponseResult tokenExpiredExceptionHandler(Exception e){
        e.printStackTrace();
        ResponseResult responseResult = new ResponseResult();
        responseResult.fail(ResponseStatus.TOKEN_ERROR);
        return responseResult;
    }


    //认证异常  AuthenticationException
    @ExceptionHandler(AuthenticationException.class)
    public ResponseResult authenticationExceptionHandler(Exception e){
        e.printStackTrace();
        ResponseResult responseResult = new ResponseResult();
        responseResult.fail(ResponseStatus.AUTHENTICATION_ERROR);
        return responseResult;
    }

    //权限异常  AuthenticationException
    @ExceptionHandler(AuthorizationException.class)
    public ResponseResult authorizationExceptionHandler(Exception e){
        e.printStackTrace();
        ResponseResult responseResult = new ResponseResult();
        responseResult.fail(ResponseStatus.AUTHORIZATION_ERROR);
        return responseResult;
    }

    // 其它异常
    @ExceptionHandler(Exception.class)
    public ResponseResult exceptionHandler(Exception e){
        e.printStackTrace();
        ResponseResult responseResult = new ResponseResult();
        responseResult.fail(ResponseStatus.FAIL);
        return responseResult;
    }

}
